package util

import "github.com/beherochuling/beesoft_api/app/model/util"

func NewEventDispatcher() *EventDispatcher {
	return &EventDispatcher{
		handlers: make(map[string][]func(util.Event)),
	}
}

type EventDispatcher struct {
	handlers map[string][]func(util.Event)
}

func (d *EventDispatcher) AddListener(eventName string, handler func(util.Event)) {
	d.handlers[eventName] = append(d.handlers[eventName], handler)
}
func (d *EventDispatcher) Dispatch(event util.Event) {
	if handlers, ok := d.handlers[event.Name]; ok {
		for _, handler := range handlers {
			handler(event)
		}
	}
}
